/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChateoController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

/**
 *
 * @author adrian
 */
public class ChatMessage {

    private String text;
    private User sender;
    private User receiver;
    private GregorianCalendar date;
    private static int nbrMessage = 0;
    private int id;

    public ChatMessage(String text, User sender, User receiver) {
        this.date = new GregorianCalendar();
        this.text = text;
        this.sender = sender;
        this.receiver = receiver;
        nbrMessage++;
        this.id = nbrMessage;

    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public User getSender() {
        return sender;
    }

    public void setSender(User sender) {
        this.sender = sender;
    }

    public User getReceiver() {
        return receiver;
    }

    public void setReceiver(User receiver) {
        this.receiver = receiver;
    }

    public GregorianCalendar getDate() {
        return date;
    }

    public void setDate(GregorianCalendar date) {
        this.date = date;
    }

    public static int getNbrMessage() {
        return nbrMessage;
    }

    public static void setNbrMessage(int nbrMessage) {
        ChatMessage.nbrMessage = nbrMessage;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        String out = format(date)+"\n";
        out += "From : "+sender.getLogin()+" \n";
        out += "To : "+receiver.getLogin()+" \n";
        out += "Message : "+ text+"\n";
        
        //TODO: If the dest has not been selected
        /*
        if(receiver.getLogin()==null){
            receiver.getLogin()=
        }
        */
       
        return out;
    }

    public static String format(GregorianCalendar calendar) {
        SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
        fmt.setCalendar(calendar);
        String dateFormatted = fmt.format(calendar.getTime());
        return dateFormatted;
    }
}
